﻿$(function () {
    $("#forgotsubmit").click(function () {
        SubmitForgot();
    });

    $("#forgotbody").keypress(function (event) {
        if (event.which == '13') {
            SubmitForgot();
        }
    });

    $("#forgotclose").click(function () {
        hideModal($("#forgotbody"));
        $("#forgotbody").remove();
    });
});

function SubmitForgot() {

    $("#forgotsubmit").hide();

    var input = {
        Email: $("#forgotemail").val()
    };

    Ajax({
        url: "/Security/TryForgot",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == true) {
                $("#content").load("/Security/ForgotComplete", input);
            } else {
                ShowForgotValidationErrors(data);
            }
        },
        complete: function () {
            $("#forgotsubmit").show();
        }
    });
}

function ShowForgotValidationErrors(data) {
    var msg = "";
    $("#forgotemailval").remove();
    if (data.IsInvalidInput) {
        if (data.EmailValidationCode > 0) {
            if (data.EmailValidationCode == 1) {
                msg = "Please enter your email";
            } else if (data.EmailValidationCode == 2) {
                msg = "Email cannot be more than 50 characters";
            } else if (data.EmailValidationCode == 3) {
                msg = "Please enter a valid email";
            }
        }
    } else if (data.DoesNotExist) {
        msg = "Email not registered in the system.";
    } else if (data.IsNotActive) {
        msg = "User is inactive.";
    }

    if (msg.length > 0) {
        $("#forgotemail").after('<span id="forgotemailval" class="input-notification error png_bg">' + msg + '</span>');
        $("#forgotemail").focus();
    }
}